package com.greateamd.timetableasy.client.mvc.view.crud.user;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.greateamd.timetableasy.client.mvc.model.Model;
import com.greateamd.timetableasy.client.services.user.UserService;
import com.greateamd.timetableasy.client.services.user.UserServiceAsync;
import com.greateamd.timetableasy.client.services.util.ServerCall;
import com.greateamd.timetableasy.client.widgets.AuthListBox;
import com.greateamd.timetableasy.client.widgets.ListBox;
import com.greateamd.timetableasy.client.widgets.MessagePopupPanel;
import com.greateamd.timetableasy.client.widgets.campus.PromotionListBox;
import com.greateamd.timetableasy.domain.Auth;
import com.greateamd.timetableasy.domain.User;

public class AddUserPage <M extends Model> extends IAddUserPage<M>{

	private final UserServiceAsync userService = GWT.create(UserService.class);

	@UiTemplate("AddUserPage.ui.xml")
	interface AddUserPageUIBinder extends UiBinder<Widget, AddUserPage> {}
	private static AddUserPageUIBinder uiBinder = GWT.create(AddUserPageUIBinder.class);

	//Attributes
	@UiField TextBox username;
	@UiField TextBox lastname;
	@UiField TextBox firstname;
	@UiField PasswordTextBox password;
	@UiField TextBox age;
	@UiField TextBox address;
	@UiField CheckBox enabled;
	//Auth
	@UiField AuthListBox listAllAuth;
	@UiField ListBox listUserAuth;
	@UiField Button addAuthBt;
	@UiField Button rmAuthBt;
	//Promotion
	@UiField PromotionListBox promotionLb;
	
	@UiField Button submitBt;
	
	/**
	 * Constructor
	 */
	public AddUserPage() {
	    initWidget(uiBinder.createAndBindUi(this));
	}
	
	@Override
	public Widget getWidgetPage() {
		return this;
	}

	@Override
	public void setUser(User user) {
	}


	//---------------------------------------------------------------------
	//-------------------------- UI action

	
	//---------------------------------------------------------------------
	//-------------------------- Handler
	
	@UiHandler("addAuthBt")
	void onAddAuthBtClick(ClickEvent e){
		if(listAllAuth.getSelectedIndex() != -1){
			listUserAuth.addItemIfNotPresent(listAllAuth.getItemText(listAllAuth.getSelectedIndex()));
		}
	}
	@UiHandler("rmAuthBt")
	void onRmAuthBtClick(ClickEvent e){
		if(listUserAuth.getSelectedIndex() != -1){
			listUserAuth.removeItem(listUserAuth.getSelectedIndex());
		}
	}
	@UiHandler("submitBt")
	void onSubmitBtClick(ClickEvent e){
		addUser();
	}
	
	//---------------------------------------------------------------------
	//-------------------------- Server call
	
	private void addUser() {
		if(!checkFormValidity()){
			Window.alert("Invalid form");
			return;
		}
		new ServerCall<Boolean>() {

			@Override
			protected void callService(AsyncCallback<Boolean> asyncCallback) {
				User userToAdd = new User(username.getText(),firstname.getText(),lastname.getText(),address.getText(),Integer.parseInt(age.getText()), password.getText(), enabled.getValue());
				for(String auth : listUserAuth.getValues()){
					userToAdd.addAuth(new Auth(userToAdd, auth));
				}
				userToAdd.setPromotion(promotionLb.getSelectedPromotion());
				userService.save(userToAdd,asyncCallback);
			}

			@Override
			public void onSuccess(Boolean success) {
				if(success)
					new MessagePopupPanel("Success","User has well been added").show();
				else
					new MessagePopupPanel("Error","Operation failed").show();
			}
		}.call();
	}
	
	private boolean checkFormValidity() {
		if(username.getText().isEmpty()) return false;
		if(lastname.getText().isEmpty()) return false;
		if(firstname.getText().isEmpty()) return false;
		if(address.getText().isEmpty()) return false;
		if(age.getText().isEmpty()) return false;
		try{
			Integer.parseInt(age.getText());
		}catch (Exception e) {
			return false;
		}
		if(password.getText().isEmpty()) return false;
		return true;
	}
}
